package com.atguigu.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
/**
 * 文件的上传下载
 * @author nrzt
 *
 */
public abstract class FileUtil {
	/**
	 * 	文件上传
	 * @param request  
	 * @param path  存放文件的路径
	 * @return
	 */
	public static boolean uploadFiles(HttpServletRequest request,String path){
			//MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
			//MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
			//上面两句可以替换此句强转
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
	        
	        //可以使用  文件的map替换  文件的list集合,并且不需要name属性
	        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
			//上面map可以替换本句，并且它不需要name属性
			//List<MultipartFile> files = multipartRequest.getFiles(fileNames);	

	        if(fileMap.isEmpty())  return false; 	       
	        for(Entry<String, MultipartFile> entry:fileMap.entrySet()){
	        	MultipartFile file = entry.getValue();
	            String fileName = file.getOriginalFilename();
	            int size = (int) file.getSize();
	            //System.out.println(fileName + "-->" + size);
	            
	            if(file.isEmpty()){
	                continue;
	            }else{        
	                File dest = new File(path + File.separator + fileName);
	                if(!dest.getParentFile().exists())  dest.getParentFile().mkdir();
	                try {
	                    file.transferTo(dest);
	                }catch (Exception e) {
	                    e.printStackTrace();
	                    return false;
	                } 
	            }
	        }
	        return true;
	    }
	
	/**
	 * 
	 * @param response
	 * @param filePath 被文件下载的路径
	 * @param filename 被下载文件的文件名	 
	 * @return
	 */
	public static boolean downLoadFiles(HttpServletResponse response,String filePath,String filename){
        File file = new File(filePath + File.separator + filename);
        if(file.exists()){ //判断文件父目录是否存在
            response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=" + filename);
            
            byte[] buffer = new byte[1024];        
            BufferedInputStream bis = null;
            OutputStream os = null; //输出流
            try {
                os = response.getOutputStream();
                bis = new BufferedInputStream(new FileInputStream(file));
                int i = -1;
                while((i =bis.read(buffer))!= -1) os.write(buffer);                                      
            } catch (Exception e) {              
            	e.printStackTrace();
                return false;
            }finally {
				try {
					if(bis!=null)bis.close();			
					if(os!=null)os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
            return true;
        }else {
        	return false;
        }
       
    }
	
}
